﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 10, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };
    //表格数据和分页
    function getList(that) {
       
        $.ajax({
            type: "POST",
            url: "/Shows/Lists",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;
                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });
        //渲染数据
        function renderDate(list) {
            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {
                    var button = '';
                    
                    //判断是否通过
                    if (item.Intone==1) {
                        item.Intone='已通过'

                    } else if (item.Intone ==2 ) {
                        item.Intone = '未通过'
                    } else if (item.Intone == 3) {
                        item.Intone = '未面试'
                    }


                    if (item.Inttwo == 1) {
                        item.Inttwo = '已通过'

                    } else if (item.Inttwo == 2) {
                        item.Inttwo = '未通过'
                    } else if (item.Inttwo == 3) {
                        item.Inttwo = '未面试'
                        button = '<a class="layui-btn  layui-btn-mini" id="update"><i class="iconfont icon-edit"></i> 修改</a>';
                    }



                    dataHtml += '<tr>'
                    + '<td>' + item.Id + '</td>'
                    + '<td align="left">' + item.Name + '</td>'
                    + '<td>' + (item.Sex == 0 ? "男" : "女") + '</td>'
                    + '<td>' + item.Age + '</td>'
                    + '<td>' + item.Nation + '</td>'
                    + '<td>' + item.Education + '</td>'
                    + '<td>' + item.IDNumber + '</td>'
                    + '<td>' + item.Phone + '</td>'
                     + '<td>' + item.Address + '</td>'
                       + '<td>' + item.AddTime + '</td>'
                        + '<td>' + item.Intone + '</td>'
                     + '<td>' + item.Inttwo + '</td>'
                      + '<td>'
                    + button
                    + '<a class="layui-btn layui-btn-mini"  id="delete" ><i class="iconfont icon-edit"></i> 删除</a>'
                    + '</td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            $('.news_list thead input[type="checkbox"]').prop("checked", false);
            form.render(); //渲染表单
        }

    }

    //分页
    function showPage(page) {
    
        laypage.render({
            elem: 'page',
            count: page.PageCount,//得到总页数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }

    
    var active = {
        setTop: function () {
            
            var that = this;
            //多窗口模式，层叠置顶
            layer.open({
                type: 1,
                title: "面试信息审批",
                skin: "layui-layer-rim",
                area: ["50%"],
                content: $("#test").html()
            });
            form.render();

        }
    }




    //修改传后台，前台接收
    var ApproveManager = {
        ApproveSubmit: function (data) {
            var _this = $(this);
            $.ajax({
                type: "POST",
                url: "/Shows/UpdateApprove",//发送请求  
                data: data.field,
                dataType: "json",
                success: function (result) {
                    if (result.State == "2")
                        {
                        layer.alert(result.Message, { icon: 7, time: 1.5 * 1000 });}
                    else if (result.State == "1") {
                        layer.msg(result.Message, { icon: 1, time: 1.5 * 1000 }, function (index) {
                            layer.closeAll();
                            getList(pageData);
                        });
                    } else
                    {
                        layer.alert(result.Message, { icon: 1 }, function (index) {
                            //添加入职信息
                            layer.open({
                                type: 2,
                                title: ['入职手续', 'background-color: #00bb9d;text-align:center;font-size:18px;color:#FFFFFF'],
                                shadeClose: true,
                                shade: [0.6, '#393D49'],
                                maxmin: false,
                                anim: 4,
                              
                                area: ['800px', '300px'],
                                content: "/Shows/Entry",
                                success: function (layero, index) {
                                    var body = layer.getChildFrame('body', index);//建立父窗体和子窗体关系                
                                    body.find('input[id="PLID"]').val(_this.attr("data-id"));//访问子窗体元素，
                                    //   注意会有延迟所以子窗体使用时要延迟加载
                                },
                                end: function () {
                                    //        如果是通过单击关闭按钮关闭弹出层，父画面没有此表单  
                                    if ($("#popupForm").length === 1) {
                                        $("#popupForm").submit();
                                    }
                                    location.reload();   //页面刷新    --只要页面关闭 刷新父页面
                                }
                            });
                        });
                    }
                        
                }
            });
        }
    }


    //删除的方法
    var deletess = {
        remove:function(Aid){
            var _this = $(this);
            layer.confirm('确定删除此信息？', { icon: 3, title: '提示信息' }, function (index) {
                var index = layer.msg('删除中，请稍候', { icon: 16, time: false, shade: 0.8 }); 
                $.ajax({
                    type: "POST",
                    url: "/Shows/DeleteApprove?id=" + Aid + "", 
                    data: JSON.stringify({ Id: _this.attr("data-id") }),
                    success: function (result) {
                     
                        var i= layer.msg(result.Message);
                        layer.close(i);   //关闭信息
                        
                        getList(pageData);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert(textStatus);
                    }
                });
                layer.close(index);

        
            });
        }
    }
      







    $(function () {
        //分页
        
        getList(pageData);

        form.on('submit(Select)', function (data) {
        
            var where = data.field;
            pageData.where = JSON.stringify(where);
            getList(pageData);
        })

        //修改界面弹窗
        $(document).on("click", "#update", function () {
            //获取ID
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            $("#idss").attr("value", Aid)
            var othis = $(this), method = othis.data('method');
           
            active.setTop();
        });
        //修改取消
        $(document).on("click", "#Cancel", function () {
            layer.closeAll();
        });

        //修改监听提交  Loginfrom为lay-filter属性
        form.on('submit(ApproveForm)', function (data) {
            
            ApproveManager.ApproveSubmit(data);

            return false;
        });

        $(document).on("click", "#delete", function () {
            //获取ID
          
      
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            deletess.remove(Aid);
            getList(pageData);
            


        });


    



    });
})